let token = localStorage.getItem('token')

layui.use('laydate', function(){
var laydate = layui.laydate;
laydate.render({ 
  elem: '#times'
  ,range: true //或 range: '~' 来自定义分割字符
});})
let flag = 1
$('#hide').on('click', function () {
  if (flag === 1) {
    $('.top').hide()
    $('.layui-icon-down').show()
    $('.layui-icon-up').hide()
    flag = 0
  } else {
    $('.top').show()
    $('.layui-icon-down').hide()
    $('.layui-icon-up').show()
    flag = 1
  }
})
$('#refresh').on('click', function () {
  window.location.reload()
})

getArticleList()
function getArticleList() {
  axios({
    method: 'get',
    url: 'http://124.223.14.236:8060/admin/ydd_article/list',
    headers: {
      "x-token": token
    },
  }).then(res => {

    let options_arr = res.data.data.data.map(item => {
      return `<option value="${item.id}">${item.catename}</option>`
    })
    // append() 追加到一个元素里面的后面去
    $('#select_cate').append(options_arr.join(''))

    // 一般当表单存在动态生成时，进行渲染
    layui.form.render() //渲染 select 选择框


    console.log(res.data.data);
    let new_arr = res.data.data.data.map((item) => {
      let new_date = item.create_date
      var date = new Date((new_date)); // 参数需要毫秒数，所以这里将秒数乘于 1000
      Y = date.getFullYear() + '-';
      M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      D = date.getDate() + ' ';
      h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':';
      m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':';
      s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds());
      let data = (Y + M + D + h + m + s)

      return `

                  <tr>
                            <td>${item.id}</td>
                            <td>${item.title}</td>
                            <td>${item.catename}</td>
                            <td></td>
                            <td>${data}</td>
                        <td>
                        <div class="layui-form-item">
                         <div class="layui-input-inline">
                           <input type="checkbox" checked lay-skin="switch" 
                           id="ishot_check"
                           data-status="${item.ishot}">
                         </div>
                        </div>
                        </td>
                        <td>
                       
                        <div class="layui-form-item">
                        <div class="layui-input-inline">
                          <input type="checkbox" checked lay-skin="switch"
                          id="ishot_check" data-status="${item.istop}">
                        </div>
                       </div>
                   
                        </td>
                        <td>${item.status}</td>
                        <td><button class="layui-btn layui-btn-normal sure" data-id="${item.id}">
                          <i class="layui-icon layui-icon-edit"></i>
                          </button>
                         <button class="layui-btn layui-btn-danger delete" data-id="${item.id}" >
                          <i class="layui-icon layui-icon-delete"></i>
                          </button></td>
                          </tr>
                  `
    })
    $('#tb').html(new_arr.join(''))
    status_show()
    layui.use('form', function () {
      var form = layui.form;
      form.render(); //更新全部
     
    })
    

  }
  )}


      
function status_show() {

  let statusAll = document.querySelectorAll('#ishot_check')
  console.log(1111);
  console.log(statusAll.length);
  console.log(statusAll);
  for(let i = 0; i < statusAll.length; i++ ){
      if(statusAll[i].dataset.status === "1"){
          statusAll[i].checked = true;
          $(statusAll[i]).next().toggleClass("layui-form-onswitch");
      }else{
          statusAll[i].checked = false;
      }
  }

}
